package edu.washington.cs.workflow.recommender;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import edu.washington.cs.workflow.analysis.guistructure.UIActionEntity;
import edu.washington.cs.workflow.analysis.guistructure.UIActionHandler;
import junit.framework.TestCase;

public class TestParsingTraceFileJEdit extends TestCase {

	public void testNextPrevBuffer() {
	    String file = "./tests/files/jedit_prev_next_buffer_new_version.txt";
	    EventTraceProcessor processor = new EventTraceProcessor(file);
	    List<UIActionHandler> handlers = processor.getAllActionHandlers();
	    
	    System.out.println("Number of handlers: " + handlers.size());
	    
	    Set<String> handlerDescs = new HashSet<String>();
		for(UIActionHandler handler : handlers) {
			handlerDescs.add(handler.methodDesc);
		}
		
		System.out.println("Unique handlers: " + handlerDescs.size());
		
		for(String handlerDesc : handlerDescs) {
			System.out.println("Handler: " + handlerDesc);
			List<String> ms = processor.getInvokedMethodsByHandler(handlerDesc);
			System.out.println("   " + ms);
		}
		
		//check the actions
		List<UIActionEntity> actions = processor.getAllUIActions();
		System.out.println("\nNumber of actions: " + actions.size());
		for(UIActionEntity action : actions) {
			System.out.println("Action: " + action);
		}
		System.out.println();
		Set<String> actionDescs = processor.getUniqueActionDesc();
		for(String actionDesc : actionDescs) {
			System.out.println("action: " + actionDesc);
			List<String> methods = processor.getInvokedMethodsByAction(actionDesc);
//			System.out.println("   invoked methods: " + methods);
			
			for(String m : methods) {
				System.out.println("   invoked: " + m);
				List<String> triggerActions = processor.getTriggeringActionDescs(m);
				System.out.println("        " + triggerActions);
			}
		}
	}
	
	//the trace file produced by the old version
	//go to the experiments, and use the following command:
	//  java -cp ./jedit-2.5-instr.jar;../bin;../libs/simmetrics_jar.jar org.gjt.sp.jedit.jEdit
	public void testJEdit_old_demo() {
		String file = "./tests/demo-traces/jedit-prev-next-buffer-old-version.txt";
		EventTraceProcessor processor = new EventTraceProcessor(file);
		
		String traceFile = "./tests/files/jedit_prev_next_buffer_new_version.txt";
		EventTraceProcessor traceProcessor = new EventTraceProcessor(traceFile);
		
		List<String> handlerDescs = processor.getAllUniqueActionHandlerDescs();
		for(String desc : handlerDescs) {
			System.out.println(desc);
			System.out.println("      actions: " + traceProcessor.getTriggeringActionDescs(desc));
			System.out.println("its invoked method: ");
			List<String> methods = processor.getInvokedMethodsByHandler(desc);
			for(String m : methods) {
				System.out.println("   " + m);
				//then query the 
				List<String> descs = traceProcessor.getTriggeringActionDescs(m);
				System.out.println("      actions: " + descs);
				
			}
		}
	}
}
